import 'package:flutter/material.dart';
import 'package:hm_pda_app/config/http_request.dart';
import 'package:hm_pda_app/components/loading_overlay.dart';

class IncomingGoodsPage extends StatefulWidget {
  const IncomingGoodsPage({super.key});

  @override
  State<IncomingGoodsPage> createState() => _IncomingGoodsPageState();
}

class _IncomingGoodsPageState extends State<IncomingGoodsPage> {
  final _formKey = GlobalKey<FormState>(); // 表单Key
  final GlobalKey<_IncomingGoodsPageState> formKey =
      GlobalKey<_IncomingGoodsPageState>();
  // 文本输入控制器
  final _carNumberController = TextEditingController();
  final _logisticsNumberController = TextEditingController();
  final _actualBoxNumberController = TextEditingController();
  final _printedNumberController = TextEditingController();
  final _remarkController = TextEditingController();
  final _noprintedNumberController = TextEditingController();
  // 对焦
  final FocusNode _focusNode = FocusNode();
  // 对焦
  final FocusNode _focusNode2 = FocusNode();

  num currentStep = 1; // 当前步骤

  // 定义一个表单对象变量
  IncomingGoods? _incomingGoods;

  // 公共方法：获取来货单信息
  Future<void> getIncomingGoodsInfo(
    BuildContext context, {
    String? licencePlate,
    String? logisticsOrderNo,
    bool whetherLastFinishOrder = false,
  }) async {
    try {
      if ((licencePlate == null || licencePlate.isEmpty) &&
          (logisticsOrderNo == null || logisticsOrderNo.isEmpty)) {
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(SnackBar(content: Text('请输入车牌号或物流单号')));
        return;
      }
      LoadingOverlay.show(context);
      final response = await HttpUtil.post(
        '/storage/incomingGoodsOperate/getLastOrder',
        data: {
          'licencePlate': licencePlate,
          'logisticsOrderNo': logisticsOrderNo,
          'whetherLastFinishOrder': whetherLastFinishOrder,
        },
      );
      print('来货响应数据--${response.data}');

      // 获取表单状态并调用方法

      if (response.data != null) {
        final mapData = response.data as Map<String, dynamic>;
        print('mapData--$mapData');
        if (mapData['data'] == null) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text('暂无数据'), duration: Duration(seconds: 3)),
          );
          return;
        }
        setIncomingGoodsForm(mapData['data']!);
      }
    } catch (e) {
      print('报错--$e');
    } finally {
      LoadingOverlay.hide();
    }
  }

  // GET请求示例,获取来货单信息
  Future<void> _getInfoRequest() async {
    await getIncomingGoodsInfo(
      context,
      licencePlate: _carNumberController.text,
      logisticsOrderNo: _logisticsNumberController.text,
    );
  }

  // 提交实际来货箱数
  static Future<void> saveBoxNum(
    BuildContext context,
    String id,
    String actualIncomingGoodsNum,
  ) async {
    try {
      if (actualIncomingGoodsNum.isEmpty || actualIncomingGoodsNum == '0') {
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(SnackBar(content: Text('请输入实际来货箱数')));
        return;
      }
      LoadingOverlay.show(context);
      // 获取表单状态并调用方法
      final response = await HttpUtil.post(
        '/storage/incomingGoodsOperate/commit',
        data: {'actualIncomingGoodsNum': actualIncomingGoodsNum, 'id': id},
      );
      print('响应数据--${response.data}');
      if (response.success) {
        print('报错--$response');
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(SnackBar(content: Text('提交成功')));
      } else {}
    } catch (e) {
      print('报错--$e');
    } finally {
      LoadingOverlay.hide();
      Navigator.of(context).pop(true);
    }
  }

  // 来货单字段操作
  void setIncomingGoodsForm(Object data) {
    // Convert data to IncomingGoods and use it
    setState(() {
      _incomingGoods = IncomingGoods.fromJson(data as Map<String, dynamic>);
    });
    print('数据复制完成,${_incomingGoods?.licencePlate}');
  }

  // 重置表单方法
  void resetForm() {
    _formKey.currentState!.reset();
    setState(() {
      _carNumberController.clear();
      _logisticsNumberController.clear();
      _actualBoxNumberController.clear();
      _printedNumberController.clear();
      _remarkController.clear();
      _noprintedNumberController.clear();
    });
  }

  @override
  void dispose() {
    // 释放控制器资源
    _carNumberController.dispose();
    _logisticsNumberController.dispose();
    _actualBoxNumberController.dispose();
    _printedNumberController.dispose();
    _remarkController.dispose();
    _noprintedNumberController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('确认来货')),
      body: _buildForm(),
      bottomNavigationBar: _buttonOprator(context),
    );
  }

  // 表单
  Widget _buildForm() {
    return Scaffold(
      body: SingleChildScrollView(
        padding: EdgeInsets.all(20),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 来货单号
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('来货单号', isRequired: false),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(_incomingGoods?.incomingGoodsNo ?? ''),
                    ),
                  ),
                ],
              ),

              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 来货日期
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('来货日期'),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(_incomingGoods?.incomingGoodsDate ?? ''),
                    ),
                  ),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),
              // 计划来货箱数
              Row(
                children: [
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('计划来货箱数'),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        _incomingGoods?.expectIncomingGoodsNum?.toString() ??
                            '0',
                      ),
                    ),
                  ),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 供应商代码
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('供应商代码'),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(_incomingGoods?.supplierCode ?? ''),
                    ),
                  ),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 供应商名称
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('供应商名称'),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(_incomingGoods?.supplierName ?? ''),
                    ),
                  ),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 来货类型
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('来货类型'),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(_incomingGoods?.businessTypeName ?? ''),
                    ),
                  ),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 采购员
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('采购员'),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(_incomingGoods?.businessTypeName ?? ''),
                    ),
                  ),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 车牌号码
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('车牌号码'),
                    ),
                  ),
                  Expanded(
                    flex: 3,
                    child:
                        (_incomingGoods?.incomingGoodsNo == null ||
                            _incomingGoods!.incomingGoodsNo!.isEmpty)
                        ? TextFormField(
                            controller: _carNumberController,
                            decoration: _buildInputDecoration(
                              hintText: '',
                              controller: _carNumberController,
                              suffixIcon:
                                  ValueListenableBuilder<TextEditingValue>(
                                    valueListenable: _carNumberController,
                                    builder: (context, value, child) {
                                      return value.text.isEmpty
                                          ? SizedBox.shrink()
                                          : IconButton(
                                              icon: Icon(Icons.clear, size: 20),
                                              onPressed: () {
                                                _carNumberController.clear();
                                              },
                                            );
                                    },
                                  ),
                            ),
                            focusNode: _focusNode,
                            onFieldSubmitted: (value) {
                              //切换焦点 _focusNode2.requestFocus();
                              _getInfoRequest();

                              print('回车执行');
                            },
                          )
                        : Text(_incomingGoods?.licencePlate ?? ''),
                  ),
                  //  Expanded(child: SizedBox()),
                  (_incomingGoods?.incomingGoodsNo == null ||
                          _incomingGoods!.incomingGoodsNo!.isEmpty)
                      ? Expanded(
                          flex: 1,
                          child: Align(
                            alignment: Alignment.centerRight,
                            child: ElevatedButton(
                              onPressed: () {
                                _getInfoRequest();
                              },
                              style: ElevatedButton.styleFrom(
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(2),
                                ),
                                padding: EdgeInsets.all(10),
                                minimumSize: Size(0, 0),
                                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              ),
                              child: Text('查询'),
                            ),
                          ),
                        )
                      : Text(''),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 物流单号
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('物流单号'),
                    ),
                  ),
                  Expanded(
                    flex: 3,

                    child:
                        (_incomingGoods?.incomingGoodsNo == null ||
                            _incomingGoods!.incomingGoodsNo!.isEmpty)
                        ? TextFormField(
                            controller: _logisticsNumberController,
                            // enabled:
                            //     _incomingGoods?.incomingGoodsNo == null ||
                            //     _incomingGoods!.incomingGoodsNo!.isEmpty,
                            decoration: _buildInputDecoration(
                              hintText: '',
                              controller: _logisticsNumberController,
                              suffixIcon:
                                  ValueListenableBuilder<TextEditingValue>(
                                    valueListenable: _logisticsNumberController,
                                    builder: (context, value, child) {
                                      return value.text.isEmpty
                                          ? SizedBox.shrink()
                                          : IconButton(
                                              icon: Icon(Icons.clear, size: 20),
                                              onPressed: () {
                                                _logisticsNumberController
                                                    .clear();
                                              },
                                            );
                                    },
                                  ),
                            ),
                            focusNode: _focusNode2,
                            onFieldSubmitted: (value) {
                              _getInfoRequest();
                              print('回车执行');
                            },
                          )
                        : Text(_incomingGoods?.logisticsOrderNo ?? ''),
                  ),
                  (_incomingGoods?.incomingGoodsNo == null ||
                          _incomingGoods!.incomingGoodsNo!.isEmpty)
                      ? Expanded(
                          flex: 1,
                          child: Align(
                            alignment: Alignment.centerRight,
                            child: ElevatedButton(
                              onPressed: () {
                                _getInfoRequest();
                              },
                              style: ElevatedButton.styleFrom(
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(2),
                                ),
                                padding: EdgeInsets.all(10),
                                minimumSize: Size(0, 0),
                                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              ),

                              child: Text('查询'),
                            ),
                          ),
                        )
                      : Text(''),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 实际来货箱数
              Row(
                children: [
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('*实际来货箱数', isRequired: true),
                    ),
                  ),
                  (_incomingGoods?.incomingGoodsNo == null ||
                          _incomingGoods!.incomingGoodsNo!.isEmpty)
                      ? Text('')
                      : Expanded(
                          flex: 2,
                          child: TextFormField(
                            controller: _actualBoxNumberController,
                            decoration: _buildInputDecoration(
                              hintText: '',
                              controller: _actualBoxNumberController,
                              suffixIcon:
                                  ValueListenableBuilder<TextEditingValue>(
                                    valueListenable: _actualBoxNumberController,
                                    builder: (context, value, child) {
                                      return value.text.isEmpty
                                          ? SizedBox.shrink()
                                          : IconButton(
                                              icon: Icon(Icons.clear, size: 20),
                                              onPressed: () {
                                                _actualBoxNumberController
                                                    .clear();
                                              },
                                            );
                                    },
                                  ),
                            ),
                          ),
                        ),
                  (_incomingGoods?.incomingGoodsNo == null ||
                          _incomingGoods!.incomingGoodsNo!.isEmpty)
                      ? Text('')
                      : Expanded(
                          flex: 1,
                          child: Align(
                            alignment: Alignment.centerRight,
                            child: ValueListenableBuilder<TextEditingValue>(
                              valueListenable: _actualBoxNumberController,
                              builder: (context, value, child) {
                                return value.text.isEmpty
                                    ? SizedBox.shrink()
                                    : ElevatedButton(
                                        onPressed: () {
                                          // _getInfoRequest();
                                          saveBoxNum(
                                            context,
                                            _incomingGoods?.id ?? '',
                                            _actualBoxNumberController.text,
                                          );
                                        },
                                        child: Text('保存'),
                                      );
                              },
                            ),
                          ),
                        ),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 补打数量
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('补打数量'),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: TextFormField(
                      controller: _noprintedNumberController,
                      decoration: _buildInputDecoration(
                        hintText: '',
                        controller: _noprintedNumberController,
                        suffixIcon: ValueListenableBuilder<TextEditingValue>(
                          valueListenable: _noprintedNumberController,
                          builder: (context, value, child) {
                            return value.text.isEmpty
                                ? SizedBox.shrink()
                                : IconButton(
                                    icon: Icon(Icons.clear, size: 20),
                                    onPressed: () {
                                      _noprintedNumberController.clear();
                                    },
                                  );
                          },
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 已打数量
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: _buildLabel('已打数量'),
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(_incomingGoods?.businessTypeName ?? ''),
                    ),
                  ),
                ],
              ),
              // SizedBox(height: 5),
              const Divider(
                height: 2, // 容器总高度2像素
                thickness: 1, // 分割线厚度5像素
                indent: 0, // 左侧缩进20像素
                endIndent: 0, // 右侧无缩进
                color: Color.fromARGB(255, 109, 108, 108), // 黑色分割线
              ),

              // 备注
              // Row(
              //   children: [
              //     Expanded(
              //       flex: 1,
              //       child: Align(
              //         alignment: Alignment.centerLeft,
              //         child: _buildLabel('备注'),
              //       ),
              //     ),
              //     Expanded(
              //       flex: 2,
              //       child: TextFormField(
              //         controller: _remarkController,
              //         decoration: _buildInputDecoration(
              //           hintText: '',
              //           controller: _remarkController,
              //           suffixIcon: ValueListenableBuilder<TextEditingValue>(
              //             valueListenable: _remarkController,
              //             builder: (context, value, child) {
              //               return value.text.isEmpty
              //                   ? SizedBox.shrink()
              //                   : IconButton(
              //                       icon: Icon(Icons.clear, size: 20),
              //                       onPressed: () {
              //                         _remarkController.clear();
              //                       },
              //                     );
              //             },
              //           ),
              //         ),
              //       ),
              //     ),
              //   ],
              // ),
              // SizedBox(height: 60),
            ],
          ),
        ),
      ),
    );
  }

  // 按钮
  Widget _buttonOprator(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(color: const Color.fromARGB(255, 77, 76, 76)),
      child: SizedBox(
        width: double.infinity,
        height: 50,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          child: Row(
            children: [
              Expanded(
                child: SizedBox(
                  height: 40,
                  child: OutlinedButton(
                    onPressed: () {
                      // 取消操作，重置表单
                      resetForm();
                    },
                    style: OutlinedButton.styleFrom(
                      foregroundColor: Colors.white,
                      backgroundColor: Colors.grey[600],
                      side: BorderSide.none,
                      textStyle: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                      ),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12),
                      ),
                      elevation: 1,
                    ),
                    child: const Text('清除'),
                  ),
                ),
              ),
              const SizedBox(width: 12),
              Expanded(
                child: SizedBox(
                  height: 40,
                  child: ElevatedButton(
                    onPressed: () {
                      // final formState = context
                      //     .findAncestorStateOfType<IncomingGoodsFormState>();
                      // formState?._submitForm();

                      showDialog(
                        context: context,
                        builder: (context) => AlertDialog(
                          title: Text('确认提交'),
                          content: Text('是否确认提交来货信息？'),
                          actions: [
                            TextButton(
                              onPressed: () => Navigator.of(context).pop(false),
                              child: const Text('取消'),
                            ),
                            TextButton(
                              onPressed: () {
                                // Navigator.of(context).pop(true);
                                _getInfoRequest();
                              },
                              child: const Text('确认'),
                            ),
                          ],
                        ),
                      );
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.blue[600],
                      foregroundColor: Colors.white,
                      textStyle: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                      ),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12),
                      ),
                      elevation: 3,
                      // shadowColor: Colors.blue.withOpacity(0.3),
                    ),
                    child: const Text('提交'),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // 构建标签文本
  Widget _buildLabel(String text, {bool isRequired = false}) {
    return SizedBox(
      // width: 200,
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 8),
        child: Text(
          text,
          style: TextStyle(
            fontWeight: FontWeight.w500,
            fontSize: 16,
            color: isRequired ? Colors.red : Colors.grey[700],
          ),
        ),
      ),
    );
  }

  // 构建输入框装饰
  InputDecoration _buildInputDecoration({
    required String hintText,
    required TextEditingController controller,
    Widget? suffixIcon,
  }) {
    print('controller ${controller.text}');
    return InputDecoration(
      hintText: hintText,
      contentPadding: EdgeInsets.zero, // 设置上下左右内边距都为0
      // prefixIcon: Icon(prefixIcon, color: Colors.grey[600]),
      suffixIcon: suffixIcon,
      hintStyle: TextStyle(color: Colors.grey[500]),
      // isDense: true 属性用于减少输入框的垂直高度，使其更紧凑
      // 当设置为 true 时，会减少内边距，让输入框看起来更小更密集
      // 适用于需要节省空间或创建更紧凑布局的场景
      // isDense: true,
      filled: true,
      fillColor: Colors.grey[50],

      // suffixIcon: controller.text.isNotEmpty
      //             ? IconButton(
      //                 icon: Icon(Icons.clear, size: 20),
      //                 onPressed: () {
      //                   controller.clear();
      //                   // 需要通知父组件状态更新
      //                   if (mounted) {
      //                     setState(() {});
      //                   }
      //                 },
      //               )
      //             : null,
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(8),
        borderSide: BorderSide(color: Colors.grey[300]!),
      ),
      enabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(8),
        borderSide: BorderSide(color: Colors.grey[300]!),
      ),
      focusedBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(8),
        borderSide: BorderSide(color: Colors.blue, width: 2),
      ),
      errorBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(8),
        borderSide: BorderSide(color: Colors.red),
      ),
      focusedErrorBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(8),
        borderSide: BorderSide(color: Colors.red, width: 2),
      ),
    );
  }
}

// 表单验证工具类
class FormValidators {
  // 必填项校验
  String? validateRequired(String? value, String fieldName) {
    if (value == null || value.trim().isEmpty) {
      return '$fieldName不能为空';
    }
    return null;
  }

  // 邮箱校验
  String? validateEmail(String? value) {
    if (value == null || value.trim().isEmpty) {
      return '邮箱不能为空';
    }
    if (!RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(value)) {
      return '请输入有效的邮箱地址';
    }
    return null;
  }

  // 手机号校验
  String? validatePhone(String? value) {
    if (value == null || value.trim().isEmpty) {
      return '手机号不能为空';
    }
    if (!RegExp(r'^1[3-9]\d{9}$').hasMatch(value)) {
      return '请输入有效的手机号码';
    }
    return null;
  }

  // 密码校验
  String? validatePassword(String? value) {
    if (value == null || value.isEmpty) {
      return '密码不能为空';
    }
    if (value.length < 6) {
      return '密码至少6个字符';
    }
    return null;
  }

  // 确认密码校验
  String? validateConfirmPassword(String? value, String password) {
    if (value == null || value.isEmpty) {
      return '请确认密码';
    }
    if (value != password) {
      return '两次输入的密码不一致';
    }
    return null;
  }
}

// 定义一个对象类
class IncomingGoods {
  final String? licencePlate; // 车牌号
  final String? incomingGoodsNo; // 来货单编号
  final bool? whetherLastFinishOrder; // 是否最后完成订单
  final String? incomingGoodsUserName; // 来货单用户名
  final String? incomingGoodsDate; // 来货单日期
  final String? supplierName;
  final String? businessTypeName;
  final num? expectIncomingGoodsNum;
  final num? actualIncomingGoodsNum;
  final String? supplierCode;
  final String? logisticsOrderNo;
  final String? id;
  IncomingGoods({
    this.licencePlate,
    this.incomingGoodsNo,
    this.whetherLastFinishOrder,
    this.incomingGoodsUserName,
    this.incomingGoodsDate,
    this.supplierName,
    this.businessTypeName,
    this.expectIncomingGoodsNum,
    this.actualIncomingGoodsNum,
    this.supplierCode,
    this.logisticsOrderNo,
    this.id,
  });
  // 手动映射
  factory IncomingGoods.fromJson(Map<String, dynamic> json) {
    return IncomingGoods(
      licencePlate: json['licencePlate']?.toString() ?? '',
      incomingGoodsNo: json['incomingGoodsNo']?.toString() ?? '',
      whetherLastFinishOrder: json['whetherLastFinishOrder'] as bool?,
      incomingGoodsUserName: json['incomingGoodsUserName']?.toString() ?? '',
      incomingGoodsDate: json['incomingGoodsDate']?.toString() ?? '',
      supplierName: json['supplierName']?.toString() ?? '',
      businessTypeName: json['businessTypeName']?.toString() ?? '',
      expectIncomingGoodsNum: json['expectIncomingGoodsNum'] as num?,
      actualIncomingGoodsNum: json['actualIncomingGoodsNum'] as num?,
      supplierCode: json['supplierCode']?.toString() ?? '',
      logisticsOrderNo: json['logisticsOrderNo']?.toString() ?? '',
      id: json['id']?.toString() ?? '',
    );
  }
}
